home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spambayes / dbmstorage.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  2.5 KB  |  91 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Wrapper to open an appropriate dbm storage type.'''
  5. from spambayes.Options import options
  6. import sys
  7. import whichdb
  8. import os
  9.  
  10. class error(Exception):
  11.     pass
  12.  
  13.  
  14. def open_db3hash(*args):
  15.     '''Open a bsddb3 hash.'''
  16.     import bsddb3
  17.     return bsddb3.hashopen(*args)
  18.  
  19.  
  20. def open_dbhash(*args):
  21.     """Open a bsddb hash.  Don't use this on Windows, unless Python 2.3 or
  22.     greater is used, in which case bsddb3 is actually named bsddb."""
  23.     
  24.     try:
  25.         import bsddb
  26.     except ImportError:
  27.         import bsddb3 as bsddb
  28.  
  29.     return bsddb.hashopen(*args)
  30.  
  31.  
  32. def open_db185hash(*args):
  33.     '''Open a bsddb185 hash.'''
  34.     import bsddb185
  35.     return bsddb185.hashopen(*args)
  36.  
  37.  
  38. def open_gdbm(*args):
  39.     '''Open a gdbm database.'''
  40.     import gdbm
  41.     return gdbm.open(*args)
  42.  
  43.  
  44. def open_best(*args):
  45.     if sys.platform == 'win32':
  46.         funcs = [
  47.             open_db3hash,
  48.             open_gdbm]
  49.         if sys.version_info >= (2, 3):
  50.             funcs.insert(0, open_dbhash)
  51.         
  52.     else:
  53.         funcs = [
  54.             open_db3hash,
  55.             open_dbhash,
  56.             open_gdbm,
  57.             open_db185hash]
  58.     for f in funcs:
  59.         
  60.         try:
  61.             return f(*args)
  62.         continue
  63.         except ImportError:
  64.             continue
  65.         
  66.  
  67.     
  68.     raise error('No dbm modules available!')
  69.  
  70. open_funcs = {
  71.     'best': open_best,
  72.     'db3hash': open_db3hash,
  73.     'dbhash': open_dbhash,
  74.     'bsddb185': open_db185hash,
  75.     'gdbm': open_gdbm }
  76.  
  77. def open(db_name, mode):
  78.     if os.path.exists(db_name):
  79.         dbm_type = whichdb.whichdb(db_name)
  80.         if sys.platform == 'win32' and sys.version_info < (2, 3) and dbm_type == 'dbhash':
  81.             dbm_type = 'db3hash'
  82.         
  83.     else:
  84.         dbm_type = options[('globals', 'dbm_type')].lower()
  85.     f = open_funcs.get(dbm_type)
  86.     if f is None:
  87.         raise error('Unknown dbm type: %s' % dbm_type)
  88.     
  89.     return f(db_name, mode)
  90.  
  91.